import Qt 4.7


Rectangle {
    id:item
    color: "#888"
    property int col;
    property int row;
    property alias wires: wires_item.children;
    property int type: 0; //1- server, 2-monitor, 0 - connector
    signal rotated;

    function isWired(ang) {
        var idx = ((360*10+ang-wires_item.rotation)/90)%4;
        return wires[idx].visible;
    }
    function hasWires() {
        return wires[0].visible||wires[1].visible||wires[2].visible||wires[3].visible;
    }
    function setWired(ang) {
        var idx = ((360*10+ang-wires_item.rotation)/90)%4;// (4-((wires_item.rotation + ang)/90))%4;
        wires[idx].visible = true;
    }
    function removeWires(ang) {
        wires[0].visible = false;
        wires[1].visible = false;
        wires[2].visible = false;
        wires[3].visible = false;
    }

    Item {
        id: wires_item;
        anchors.fill: item;
        Repeater {
            model: 4
            Rectangle {
                color: middleRect.color;
                width: item.width/2;
                height: 4;
                anchors.left: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                transformOrigin: Item.Left
                rotation: index*90
                visible: false
            }
        }
        NumberAnimation on rotation {duration: 250;id:anim1;running:false;
            easing.type : Easing.OutCubic
            onCompleted: {
                wires_item.rotation = (wires_item.rotation+360)%360;
                if (ma.pressed) {
                    anim1.to = (wires_item.rotation+(ma.pressedButtons==Qt.RightButton?-90:90));
                    anim1.running = true;
                } else {
                    item.rotated();
                }
            }
        }
    }
    Rectangle {
        id: middleRect
        width: 6;
        height: 6;
        anchors.centerIn: parent
        color: "#aaa";
        visible: type==0;
    }

    Image {
        id: img_srv;
        source: "pics/server.png";
        anchors.centerIn: parent;
        visible: type==1;
        smooth: true;
        PropertyAnimation { target:img_srv ;property: "rotation"; loops: Animation.Infinite;from:0; to: 360;running: true; duration: 2000 }

    }
    Image {
        id: img_off;
        source: "pics/house_off.png";
        anchors.centerIn: parent;
        visible: type==2;
        opacity: 1;
    }
    Image {
        id: img_on;
        source: "pics/house_on.png";
        anchors.centerIn: parent;
        visible: type==2;
        opacity: 0;

    }


    transitions:
        Transition {
            ColorAnimation { target:middleRect;  duration: 500;easing.type : Easing.OutBack }
            PropertyAnimation{ target:img_on; properties:"opacity";  duration: 500;}}

    states: [
        State {
            name: "On"
            PropertyChanges{ target: middleRect; color:"#cdf";}
            PropertyChanges{ target: img_on; opacity:1}
        }
    ]

    MouseArea {
        id: ma;
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onPressed: {
            if (hasWires()) {
                anim1.running = true;
                anim1.complete()
            }
        }
    }
}
